package com.hkd.blogsystem.config;

import com.hkd.blogsystem.enums.ResultCodeEnum;
import com.hkd.blogsystem.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.resource.NoResourceFoundException;

@Slf4j
@RestControllerAdvice
public class ExceptionAdvice {
    @ExceptionHandler
    public Result handler(Exception e){
        log.error("异常, e:",e);
        return Result.fail(e.getMessage());
    }

    @ExceptionHandler
    public Result handler(UnsupportedOperationException e){
        log.error("异常, e:{}",e.getMessage());
        return Result.fail(ResultCodeEnum.PARAM_INVLID.getCode(),e.getMessage());
    }

    @ExceptionHandler
    public Result handler(NoResourceFoundException e){
        log.error("异常, e:{}",e.getResourcePath());
        return Result.fail(ResultCodeEnum.NO_RESOURCES.getCode(),e.getResourcePath());
    }

    @ExceptionHandler
    public Result handler(MethodArgumentNotValidException e){
        log.error("参数校验不通过, e:", e.getMessage());
        return Result.fail(ResultCodeEnum.PARAM_INVLID.getCode(), e.getMessage());
    }
}
